package com.qianguo.sender.usercenter;

import android.text.TextUtils;
import android.widget.Button;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.api.RequetType;
import com.eva.androidlibrary.api.entity.UserInfo;
import com.eva.androidlibrary.widget.TimerWidget;
import com.eva.androidlibrary.widget.mdedittext.MaterialEditText;
import com.qianguo.sender.R;
import com.qianguo.sender.SenderMainActivity;
import com.qianguo.sender.base.BaseTitleActivity;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * 配送员注册页面
 *
 * @author eva
 * @date 16/4/28
 */
@EActivity(R.layout.activity_sender_register)
public class RegisterActivity extends BaseTitleActivity {

    /** 注册手机号 */
    @ViewById(R.id.sender_register_mobile_edit_text)
    MaterialEditText mEdtMobile;

    /** 手机验证码 */
    @ViewById(R.id.sender_register_auth_code_edit_text)
    MaterialEditText mEdtAuthCode;

    /** 密码 */
    @ViewById(R.id.sender_register_password_edit_text)
    MaterialEditText mEdtPassword;

    /** 确认密码 */
    @ViewById(R.id.sender_register_password_confirm_edit_text)
    MaterialEditText mEdtConfirmPassword;

    /** 推荐人邀请码 */
    @ViewById(R.id.sender_register_referrer_edit_text)
    MaterialEditText mEdtReferrer;

    /** 获取验证码按钮 */
    @ViewById(R.id.sender_register_timer_widget)
    TimerWidget mTimer;

    /** 注册按钮 */
    @ViewById(R.id.sender_register_button)
    Button mBtnRegister;

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.sender_register_title);
    }

    @Override
    protected void initializeUI() {

    }

    @Click(R.id.sender_register_button)
    void clickRegister() {
        String mobile = mEdtMobile.getText().toString();
        String authCode = mEdtAuthCode.getText().toString();
        String password = mEdtPassword.getText().toString();
        String confirmPassword = mEdtConfirmPassword.getText().toString();
        String referrer = mEdtReferrer.getText().toString();
        if (validateInfo(mobile, authCode, password, confirmPassword)) {
            register(mobile, authCode, password, referrer);
        }
    }

    @Background
    void register(String mobile, String authCode, String password, String referrer) {
        mApp.getApi().register(mobile, password, "", RequetType.AUTH_CODE_SMS, authCode, "", referrer, "", new HttpRequestCallback<UserInfo>() {
            @Override
            public void onSuccess(int code, String msg, UserInfo data) {
                SenderMainActivity.jumpToMain(RegisterActivity.this);
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(RegisterActivity.this, msg);
            }
        });
    }

    private boolean validateInfo(String mobile, String authCode, String password, String confimPassword) {
        if (TextUtils.isEmpty(mobile)) {
            mEdtMobile.setError(getString(R.string.sender_register_error_mobile));
            return false;
        }

        if (TextUtils.isEmpty(authCode)) {
            mEdtAuthCode.setError(getString(R.string.sender_register_error_auth_code));
            return false;
        }

        if (TextUtils.isEmpty(password) || password.length() < 6 || password.length() > 20) {
            mEdtPassword.setError(getString(R.string.sender_register_error_password));
            return false;
        }

        if (TextUtils.isEmpty(confimPassword) || !confimPassword.equals(password)) {
            mEdtConfirmPassword.setError(getString(R.string.sender_register_error_confirm_password));
            return false;
        }
        return true;
    }
}
